package com.vm.commons;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import android.os.Environment;
import android.util.Log;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2014年10月13日 上午10:49:01]
 * @Version: [v1.0]
 * 
 */
public class StringUtil {

	private static final String TAG = "StringUtil";

	public static String DEVICEID = null;

	/**
	 * 组装String,sql语句中的@#xx 和HashMap中的key值匹配,并将values值替换掉@#xx
	 * 
	 * @param 原始语句中带
	 *            "@#"参数SQL语句,如name=@#name
	 * @param 参数键值对集合如hashMap
	 *            .Add(@#name,"123")
	 * @return
	 */
	public static String packageString(String sql,
			HashMap<String, String> hashMap) {
		try {
			if (sql.contains("@#")) {
				for (int i = 0; i < sql.length(); i++) {
					if (sql.indexOf("@#") > 0) {
						String zhanweifu = "";
						// 当替换掉一个参数时，第一个@将被替换掉，所以又将重新开始索引
						int start = sql.indexOf("@#");
						int lastat = sql.lastIndexOf("@#");
						// if (start == lastat) {

						int kuohao = sql.indexOf(")", start);
						int douhao = sql.indexOf(",", start);
						int space = sql.indexOf(" ", start);

						int[] fuhao = { kuohao, douhao, space };
						int temp;
						for (int m = 0; m < fuhao.length - 1; m++) {

							for (int n = m + 1; n < fuhao.length; n++) {
								if (fuhao[m] > fuhao[n]) {
									temp = fuhao[m];
									fuhao[m] = fuhao[n];
									fuhao[n] = temp;
								}
							}
						}
						// 如果后面存在符号则从start截取到第一个符号,否则从start取到末尾
						if (fuhao[2] > 0) {
							for (int j : fuhao) {
								if (j > 0) {
									zhanweifu = sql.substring(start, j);
									break;
								}
							}
						} else {
							zhanweifu = sql.substring(start);
						}

						if (start == lastat) {
							i = start + zhanweifu.length();
						} else {
							i = start + 1;
						}
						// }
						// else {
						// int space = sql.indexOf(" ", start);
						// int douhao = sql.indexOf(",", start);
						// int last = 0;
						// if (space > 0 && douhao > 0) {
						//
						// // int netat = str.IndexOf("@", start);
						// last = space > 0 && space > douhao ? douhao
						// : space;
						// } else if (space > 0 && douhao <= 0) {
						// last = space;
						// } else if (space <= 0 && douhao > 0) {
						// last = douhao;
						// }
						//
						// zhanweifu = sql.substring(start, last);
						// i = start + 1;
						// }
						if (hashMap.get(zhanweifu) == null) {

							sql = sql.replaceFirst(zhanweifu, "''");
						} else {
							String value = "";
							if (hashMap.get(zhanweifu).indexOf("'") > 0) {
								value = hashMap.get(zhanweifu).replace("'",
										"\"");
							} else {
								value = hashMap.get(zhanweifu);
							}

							sql = sql
									.replaceFirst(zhanweifu, "'" + value + "'");
						}
					}
				}
			}
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}

		return sql;
	}

	// public static String getDateDir(String filepath) {
	// Calendar calendar = Calendar.getInstance();
	// String yearString = Integer.toString(calendar.get(Calendar.YEAR));
	// String monthString = Integer.toString(calendar.get(Calendar.MONTH) + 1);
	//
	// File file = new File(MyConfig.FILEPATH + filepath);
	// if (!file.exists())
	// file.mkdirs();
	// String ym = yearString + monthString;
	// filepath += "/" + ym + "/";
	// file = new File(MyConfig.FILEPATH + filepath);
	// if (!file.exists())
	// file.mkdirs();
	// filepath += calendar.get(Calendar.DAY_OF_MONTH) + "/";
	// file = new File(MyConfig.FILEPATH + filepath);
	// if (!file.exists())
	// file.mkdirs();
	//
	// return filepath;
	// }

	/**
	 * 返回当前日期
	 * 
	 * @return
	 */
	public static String getDate() {
		return getDate(1);
	}

	/**
	 * 返回当前日期
	 * 
	 * @param 日期格式
	 * @type 值
	 * @param 1. yyyy-MM-dd HH:mm:ss
	 * @param 2. yyyyMMddHHmmss
	 * @param 3. yyyyMMdd
	 * @param 4. yyyy-MM-dd
	 * @return
	 */
	public static String getDate(int type) {
		Date date = new Date();
		SimpleDateFormat dateformat = null;
		switch (type) {
		case 1:
			dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			break;
		case 2:
			dateformat = new SimpleDateFormat("yyyyMMddHHmmss");
			break;
		case 3:
			dateformat = new SimpleDateFormat("yyyyMMdd");
			break;
		case 4:
			dateformat = new SimpleDateFormat("yyyy-MM-dd");
			break;
		default:
			break;
		}

		return dateformat.format(date);
	}

	/**
	 * 获取SDK版本
	 * 
	 * @param sdkversion
	 * @return
	 */
	public static String getVersion(int sdkversion) {
		String version = "2.2";
		switch (sdkversion) {
		case 3:
			version = "1.5";
			break;
		case 4:
			version = "1.6";
			break;
		case 7:
			version = "2.1";
			break;
		case 8:
			version = "2.2";
			break;
		case 10:
			version = "2.3.3";
			break;
		case 11:
			version = "3.0";
			break;
		case 12:
			version = "3.1";
			break;
		case 13:
			version = "3.2";
			break;
		case 14:
			version = "4.0";
			break;
		case 15:
			version = "4.0.3";
			break;
		case 16:
			version = "4.1.2";
			break;
		default:
			break;
		}
		return version;

	}

	public static String getShotName(String name) {
		String shotName = "";
		try {

			if (name.getBytes("GB2312").length >= 10) {
				char[] names = name.toCharArray();
				int i = 0;
				String nowchar = "" + names[0];

				while ((shotName.getBytes("GB2312").length + nowchar
						.getBytes("GB2312").length) < 9) {
					shotName += names[i];
					nowchar = "" + names[i + 1];
					i++;
				}
				shotName += "..";
			} else {
				shotName = name;
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return shotName;
	}

	/**
	 * 获取Apk在SD卡中的绝对路径
	 * 
	 * @return
	 */
	public static String getSdApkUrl(String softId) {
		return Environment.getExternalStorageDirectory().getPath()
				+ "/vmclient/" + softId + ".apk";
	}

	/**
	 * 获取Apk在SD卡中的绝对路径
	 * 
	 * @return
	 */
	public static String getFilePath() {
		return Environment.getExternalStorageDirectory().getPath()
				+ "/vmclient/myfiles/";
	}

	public static void main(String[] args) {
		System.out.println(StringUtil.getShotName("你好吗其亲亲111"));
	}
}
